package com.wuliqing.android.zte.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.wuliqing.android.zte.data.ContactData;
import com.wuliqing.android.zte.drawable.RoundImageDrawable;
import com.wuliqing.android.zte.myapplication.R;

import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    private Context mContext;
    private List<ContactData> mContactDataList;
    public RecyclerViewAdapter(Context mContext, List<ContactData> list) {
        this.mContext = mContext;
        mContactDataList = list;
    }

    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =
                LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_card_main, parent, false);
        return new ViewHolder(view);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(final RecyclerViewAdapter.ViewHolder holder, int position) {
        final View view = holder.mView;
        holder.position = position;
        ContactData contactData = mContactDataList.get(position);
        holder.name_v.setText(contactData.getContact_name());
//        if (contactData.getContact_number() != null) {
//            holder.number_v.setVisibility(View.VISIBLE);
//            holder.number_v.setText(contactData.getContact_number());
//        }else {
//            holder.number_v.setVisibility(View.GONE);
//        }
//        if (contactData.getContact_email() != null){
//            holder.email_v.setVisibility(View.VISIBLE);
//            holder.email_v.setText(contactData.getContact_email());
//        }else {
//            holder.email_v.setVisibility(View.GONE);
//        }
        if (contactData.getContact_photo_bitmap() != null) {
            holder.photo_v.setImageDrawable(new RoundImageDrawable(contactData.getContact_photo_bitmap()));
        } else {
            Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(),
                    R.drawable.cheese_3);
            holder.photo_v.setImageDrawable(new RoundImageDrawable(bitmap));
        }
    }

    @Override
    public int getItemCount() {
        return mContactDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        public final View mView;
        public int position;
        public TextView name_v;
//        public TextView number_v;
//        public TextView email_v;
        public ImageView photo_v;
//        public Button call_v;
        public ViewHolder(View view) {
            super(view);
            mView = view;
            mView.setOnClickListener(this);
            mView.setOnLongClickListener(this);
            name_v = (TextView)view.findViewById(R.id.contact_name);
//            number_v = (TextView)view.findViewById(R.id.contact_number);
//            email_v = (TextView)view.findViewById(R.id.contact_others_msg);
            photo_v = (ImageView)view.findViewById(R.id.contact_photo_img);
//            call_v = (Button)view.findViewById(R.id.contact_call_btn);
//            call_v.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (null != onRecyclerViewListener) {
//                        onRecyclerViewListener.onCallButtonClick(position);
//                    }
//                }
//            });
            photo_v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != onRecyclerViewListener){
                        onRecyclerViewListener.onSelectImageClick(position);
                    }
                }
            });
        }

        @Override
        public void onClick(View v) {
            if (null != onRecyclerViewListener) {
                onRecyclerViewListener.onItemClick(position);
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (null != onRecyclerViewListener) {
                return onRecyclerViewListener.onItemLongClick(position);
            }
            return false;
        }
    }

    public interface OnRecyclerViewListener {
        void onItemClick(int position);

        boolean onItemLongClick(int position);

        void onCallButtonClick(int position);

        void onSelectImageClick(int position);
    }

    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }
}
